package cn.hengzhu.main_manager.web.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hengzhu.main_manager.repository.dao.AccessRecordMapper;
import cn.hengzhu.main_manager.repository.dao.ManageCaseBoxMapper;
import cn.hengzhu.main_manager.repository.dao.ManageCaseMapper;
import cn.hengzhu.main_manager.repository.domain.AccessRecord;
import cn.hengzhu.main_manager.repository.domain.AccessRecordExample;
import cn.hengzhu.main_manager.repository.domain.ManageCase;
import cn.hengzhu.main_manager.repository.domain.ManageCaseBox;
import cn.hengzhu.main_manager.repository.domain.ManageCaseBoxExample;
import cn.hengzhu.main_manager.repository.domain.ManageUser;
import cn.hengzhu.main_manager.repository.vo.OneWeekRecordVo;
import cn.hengzhu.main_manager.repository.vo.UseNumByDayVo;
import cn.hengzhu.main_manager.utils.UserContext;

@Controller
@RequestMapping("/main")
public class StatisticalController {

	@Autowired
	private ManageCaseMapper manageCaseMapper;
	@Autowired
	private ManageCaseBoxMapper manageCaseBoxMapper;
	@Autowired
	private AccessRecordMapper accessRecordMapper;

	@ResponseBody
	@RequestMapping(value = "/statistical", method = RequestMethod.GET)
	public OneWeekRecordVo statistical() {
		// 储物柜总数、
		int caseNum = 0;
		// 储物柜的总箱子数
		int boxNum = 0;
		// 链接不正常的箱子数量
		int errBoxNum = 0;
		// 未关门的箱子数量
		int notLockBoxNum = 0;
		OneWeekRecordVo oneWeekRecordVo = new OneWeekRecordVo();
		List<UseNumByDayVo> useList = new ArrayList<>();
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.WEEK_OF_YEAR, -1);
		Date startTime = calendar.getTime();
		ManageUser user = UserContext.getUser();
		Integer compId = user.getCompId();
		Byte useType=0;
		List<ManageCase> caseList = manageCaseMapper.getListNumByCompId(compId,useType);
		List<String> caseNoList = caseList.stream()
				.collect(Collectors.mapping(ManageCase::getCaseNo, Collectors.toList()));
		if (caseNoList != null && !caseNoList.isEmpty()) {
			// SELECT
			// <include refid="Base_Column_List" />
			// from manage_case_box
			// WHERE CASE_NO in
			// <foreach collection="list" item="caseNo" open="(" close=")"
			// separator=",">
			// #{caseNo}
			// </foreach>
			// and IS_DEL=0
			// order by DOOR_NUM
			ManageCaseBoxExample example = new ManageCaseBoxExample();
			example.setOrderByClause("DOOR_NUM");
			example.createCriteria().andIsDelEqualTo((byte) 0).andCaseNoIn(caseNoList);
			List<ManageCaseBox> caseBoxList = manageCaseBoxMapper.selectByExample(example);

			List<Integer> boxIdList = caseBoxList.stream()
					.collect(Collectors.mapping(ManageCaseBox::getId, Collectors.toList()));
			List<ManageCaseBox> notIsConnList = caseBoxList.stream().filter(a -> a.getIsConn() == 0)
					.collect(Collectors.toList());
			List<ManageCaseBox> notLockList = caseBoxList.stream().filter(a -> a.getIsLocked() == 0)
					.collect(Collectors.toList());
			// 储物柜总数、
			caseNum = caseList.size();
			// 储物柜的总箱子数
			boxNum = caseList.stream().mapToInt(ManageCase::getBoxSum).sum();
			// 链接不正常的箱子数量
			errBoxNum = notIsConnList.size();
			// 未关门的箱子数量
			notLockBoxNum = notLockList.size();
			// 一周内箱子使用情况
			if (boxIdList != null && !boxIdList.isEmpty()) {

				AccessRecordExample recordExample = new AccessRecordExample();
				recordExample.createCriteria().andCreateTimeGreaterThan(startTime).andCreateTimeLessThan(date)
						.andTypeEqualTo((byte) 1).andBoxIdIn(boxIdList);
				List<AccessRecord> accessRecordList = accessRecordMapper.selectByExample(recordExample);

				Map<String, List<AccessRecord>> createDayMap = accessRecordList.stream()
						.collect(Collectors.groupingBy(AccessRecord::getCreateDay));
				Set<String> keySet = createDayMap.keySet();
				for (String string : keySet) {
					List<AccessRecord> list = createDayMap.get(string);
					UseNumByDayVo useNumByDayVo = new UseNumByDayVo();
					useNumByDayVo.setUseDate(string);
					useNumByDayVo.setUseNum(list.size());
					useList.add(useNumByDayVo);
				}
			}

		}
		oneWeekRecordVo.setBoxNum(boxNum);
		oneWeekRecordVo.setCaseNum(caseNum);
		oneWeekRecordVo.setErrBoxNum(errBoxNum);
		oneWeekRecordVo.setNotLockBoxNum(notLockBoxNum);
		oneWeekRecordVo.setList(useList);
		return oneWeekRecordVo;
	}
}
